package com.lz.ionicbase.engine;

import android.content.Context;

import com.google.gson.Gson;
import com.lz.ionicbase.engine.callback.AppCallback;
import com.lz.ionicbase.model.CheckUpgradeBean;
import com.lz.ionicbase.model.SimpleBean;
import com.lz.ionicbase.ui.activity.action.IFeedBackActivity;
import com.lz.ionicbase.ui.activity.action.ISetActivity;
import com.lzy.okgo.OkGo;

import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by wyfei on 2017/2/9.
 */
public class SetHelper extends BaseHelper{

    private static final String TAG = "SetHelper";
    private Context context;
    private ISetActivity iSetActivity;
    private IFeedBackActivity iFeedBackActivity;

    public SetHelper(ISetActivity iAction) {
        super(iAction, (Context) iAction);
        iSetActivity = iAction;
        context = (Context) iAction;
    }
    public SetHelper(IFeedBackActivity iAction) {
        super(iAction, (Context) iAction);
        iFeedBackActivity = iAction;
        context = (Context) iAction;
    }

    public void setIsNotice(String url, Map<String, String> param) {

        getData(url, param, new AppCallback<SimpleBean>(context) {
            @Override
            public void onSuccess(SimpleBean resObj, Call call, Response response) {
                if(resObj.code ==100){
                    iSetActivity.onSaveIsNoticeSuccess();
                }else {
                    iSetActivity.onSaveIsNoticeError(null);
                    iSetActivity.onDataException();
                }
            }
            @Override
            public SimpleBean convertSuccess(Response response) throws Exception {
                SimpleBean resObj = new Gson().fromJson(response.body().string(), SimpleBean.class);
                return resObj;
            }
            @Override
            public void onError(Call call, Response response, Exception e) {
                super.onError(call, response, e);
                iSetActivity.onSaveIsNoticeError(e);
            }
        });

    }

    public void checkUpgrade(String url, Map<String, String> param) {

        getData(url, param, new AppCallback<CheckUpgradeBean>(context) {
            @Override
            public void onSuccess(CheckUpgradeBean resObj, Call call, Response response) {
                if(resObj.code ==100 && resObj.upgrade){
                    iSetActivity.onNeedUpgrade(resObj.surl);
                }else {
                    iSetActivity.onLastVersion();
                }
            }
            @Override
            public CheckUpgradeBean convertSuccess(Response response) throws Exception {
                CheckUpgradeBean resObj = new Gson().fromJson(response.body().string(), CheckUpgradeBean.class);
                return resObj;
            }
        });

    }

    public void feedBack(String url, Map<String, String> param) {

        getData(url, param, new AppCallback<SimpleBean>(context) {
            @Override
            public void onSuccess(SimpleBean resObj, Call call, Response response) {
                if(resObj.code ==100){
                    iFeedBackActivity.onFeedBackSuccess();
                }else {
                    iFeedBackActivity.onDataException();
                }
            }
            @Override
            public SimpleBean convertSuccess(Response response) throws Exception {
                SimpleBean resObj = new Gson().fromJson(response.body().string(), SimpleBean.class);
                return resObj;
            }
        });

    }

    protected void onError(){

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);

        iSetActivity = null;
        iFeedBackActivity = null;
        context = null;
    }
}
